<?php
require_once 'config.php';
require_once 'AlibabaSDK.php';
require_once 'Database.php';

// 初始化SDK和数据库连接
$sdk = new AlibabaSDK(APP_KEY, APP_SECRET);
$db = new Database(DB_HOST, DB_USER, DB_PASS, DB_NAME);

// 设置access_token
$accessToken = '33b3796c-4274-4a41-8aab-92e83df98498';

// 配置参数
$BATCH_SIZE = 2; // 每批处理2个商品
$PAGE_SIZE = 2;  // 每页2个商品
$SLEEP_TIME = 2; // 请求间隔时间(秒)

// 获取上次处理的位置
function getLastPosition() {
    $file = 'crawler_position.txt';
    if (file_exists($file)) {
        $data = json_decode(file_get_contents($file), true);
        return $data ?? ['category_id' => 0, 'page' => 1];
    }
    return ['category_id' => 0, 'page' => 1];
}

// 保存处理位置
function savePosition($categoryId, $page) {
    $data = [
        'category_id' => $categoryId,
        'page' => $page,
        'update_time' => date('Y-m-d H:i:s')
    ];
    file_put_contents('crawler_position.txt', json_encode($data));
}

// 获取待处理的二级分类
function getLevel2Categories($db, $lastCategoryId = 0) {
    try {
        // 修改SQL，只获取未处理的二级分类
        $sql = "SELECT id, title as name,parent_id 
                FROM category 
                WHERE parent_id > 0 
                AND (is_sync = 0 OR is_sync IS NULL)
                ORDER BY id ASC 
                LIMIT 1";
        
        $result = $db->getConnection()->query($sql);
        
        if (!$result) {
            throw new Exception("查询失败: " . $db->getConnection()->error);
        }
        
        echo "获取到的未处理二级分类:\n";
        while ($row = $result->fetch_assoc()) {
            print_r($row);
        }
        
        // 重置结果集指针
        $result->data_seek(0);
        
        return $result;
        
    } catch (Exception $e) {
        echo "获取二级分类出错: " . $e->getMessage() . "\n";
        return false;
    }
}

// 获取商品详情
function getProductDetail($sdk, $offerId, $accessToken) {
    try {
        $params = [
            'offerDetailParam' => json_encode([
                'offerId' => $offerId,
                'country'=>'ru',
                'outMemberId' => '23423532fwef'
            ], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)
        ];
        
        return $sdk->execute(
            'com.alibaba.fenxiao.crossborder',
            'product.search.queryProductDetail',
            $params,
            $accessToken
        );
    } catch (Exception $e) {
        echo "获取商品详情出错：" . $e->getMessage() . "\n";
        return false;
    }
}

// 保存商品和SKU
function saveProductAndSku($db, $product, $categoryId) {
    try {
        // 1. 保存商品基本信息
        $productData = [
            'name' => $product['subject'] ?? '',
            'sn' => $product['offerId'] ?? '',
            'cid' => $categoryId,
            'market_price' => $product['price'] ?? 0,
            'shop_price' => $product['price'] ?? 0,
            'cost_price' => $product['price'] ?? 0,
            'stock' => $product['quantity'] ?? 0,
            'thumb' => $product['imageUrl'] ?? '',
            'images' => implode(',', $product['imageUrls'] ?? []),
            'create_time' => time(),
            'update_time' => time(),
            'status' => 1,
            'is_sale' => 1
        ];
        
        // 保存商品并获取商品ID
        $goodsId = $db->saveProduct($productData);
        
        if (!$goodsId) {
            throw new Exception("保存商品失败");
        }
        
        // 2. 保存SKU信息
        if (isset($product['skuInfos']) && !empty($product['skuInfos'])) {
            foreach ($product['skuInfos'] as $sku) {
                $skuData = [
                    'goods_id' => $goodsId,
                    'key' => $sku['skuId'] ?? '',
                    'specs' => json_encode($sku['attributes'] ?? [], JSON_UNESCAPED_UNICODE),
                    'key_name' => $sku['attributes'][0]['attributeValue'] ?? '',
                    'shop_price' => $sku['price'] ?? 0,
                    'market_price' => $sku['price'] ?? 0,
                    'cost_price' => $sku['price'] ?? 0,
                    'stock' => $sku['quantity'] ?? 0,
                    'sku_sn' => $sku['skuCode'] ?? '',
                    'update_time' => time()
                ];
                
                $db->saveSku($skuData);
            }
        }
        
        return true;
    } catch (Exception $e) {
        echo "保存商品和SKU出错：" . $e->getMessage() . "\n";
        return false;
    }
}

// 批量保存商品
function saveBatchProducts($db, $products, $categoryId, $sdk, $accessToken) {
    foreach ($products as $product) {
        echo "处理商品: {$product['subject']} (ID: {$product['offerId']})\n";
        
        // 检查商品是否已处理
        $checkSql = "SELECT id, is_processed FROM products WHERE offerId = ?";
        $stmt = $db->getConnection()->prepare($checkSql);
        $stmt->bind_param('s', $product['offerId']);
        $stmt->execute();
        $result = $stmt->get_result();
        $existingProduct = $result->fetch_assoc();
        $stmt->close();

        if ($existingProduct && $existingProduct['is_processed'] == 1) {
            echo "商品 {$product['offerId']} 已处理，跳过\n";
            continue;
        }
        
        // 获取商品详情
        $detail = getProductDetail($sdk, $product['offerId'], $accessToken);
        
        if (!$detail) {
            echo "获取商品详情失败，跳过\n";
            continue;
        }
        
        if (isset($detail['result']) && isset($detail['result']['result'])) {
            $productDetail = $detail['result']['result'];
            // 合并基础信息和详情信息
            $fullProduct = array_merge($product, $productDetail);
            
            // 保存商品和SKU
            if ($db->saveProduct($fullProduct, $categoryId)) {
                echo "商品保存成功\n";
            } else {
                echo "商品保存失败\n";
            }
            
            sleep(1); // 避免请求过快
        } else {
            echo "商品详情格式错误，跳过\n";
        }
    }
}

// 搜索商品
function searchProducts($sdk, $db, $categoryName, $categoryId, $accessToken, $startPage = 1,$parentname) {
    try {
        $processedCount = 0;
        $currentPage = $startPage;
        
        do {
            $params = [
                'offerQueryParam' =>json_encode( [
                    'keyword' => $parentname.$categoryName,
                    'beginPage' => (string)$currentPage,
                    'pageSize' => '50',
                    'sort' => json_encode(['price' => 'asc']),
                    'outMemberId' => '23423532fwef',
                    'country' => 'ru',
                    'priceEnd'=>20,
                    'keywordTranslate' => false
                ],JSON_UNESCAPED_UNICODE)
            ];
            $response = $sdk->execute(
                'com.alibaba.fenxiao.crossborder', 
                'product.search.keywordQuery',
                $params,
                $accessToken
            );
            
          
            
            if (!isset($response['result']['result']['data']) || !is_array($response['result']['result']['data'])) {
                echo "没有找到商品数据\n";
                return 0;
            }
            $total = $response['result']['result']['totalRecords'];
            $products = $response['result']['result']['data'];
            $totalCount = count($products);
            echo "获取到 {$totalCount} 个商品\n";
            
            // 处理所有未处理的商品
            foreach ($products as $product) {
                try {
                    if($product['priceInfo']['price']>20){
                        file_put_contents("log.txt",'价格大于20\n',FILE_APPEND);
                        continue;
                    }
                    // 检查商品是否已处理
                    $checkSql = "SELECT id, is_processed FROM products WHERE offerId = ?";
                    echo "准备执行SQL: " . $checkSql . "\n";
                    echo "商品ID: " . $product['offerId'] . "\n";
                    
                    // 检查数据库连接
                    if (!$db->getConnection()) {
                        throw new Exception("数据库连接已断开");
                    }
                    
                    $stmt = $db->getConnection()->prepare($checkSql);
                    if ($stmt === false) {
                        throw new Exception("预处理SQL失败: " . $db->getConnection()->error);
                    }
                    
                    $offerId = $product['offerId'];
                    if (!$stmt->bind_param('s', $offerId)) {
                        throw new Exception("绑定参数失败: " . $stmt->error);
                    }
                    
                    if (!$stmt->execute()) {
                        throw new Exception("执行SQL失败: " . $stmt->error);
                    }
                    
                    $result = $stmt->get_result();
                    if ($result === false) {
                        throw new Exception("获取结果失败: " . $stmt->error);
                    }
                    
                    $existingProduct = $result->fetch_assoc();
                    $stmt->close();

                    if ($existingProduct && $existingProduct['is_processed'] == 1) {
                        echo "商品 {$product['offerId']} 已处理，跳过\n";
                        continue;
                    }
                    
                    // 获取商品详情并保存
                    $detail = getProductDetail($sdk, $product['offerId'], $accessToken);
                    if ($detail && isset($detail['result']) && isset($detail['result']['result'])) {
                        $productDetail = $detail['result']['result'];
                        $fullProduct = array_merge($product, $productDetail);
                        
                        if ($db->saveProduct($fullProduct, $categoryId)) {
                            echo "商品 {$product['offerId']} 保存成功\n";
                            $processedCount++;
                        }
                    }
                    
                    sleep(1); // 避免请求过快
                } catch (Exception $e) {
                    echo "处理商品 {$product['offerId']} 出错: " . $e->getMessage() . "\n";
                    // 尝试重新连接数据库
                    try {
                        $db = new Database(DB_HOST, DB_USER, DB_PASS, DB_NAME);
                    } catch (Exception $e) {
                        echo "重新连接数据库失败: " . $e->getMessage() . "\n";
                        return false;
                    }
                    continue;
                }
            }
            
            // 保存当前进度
            savePosition($categoryId, $currentPage);
      
            // 检查是否还有下一页
            $totalPages = $response['result']['result']['totalPage'];
            echo "总页数: {$totalPages}, 当前页: {$currentPage}\n";
            
            if ($currentPage >= $totalPages || $currentPage >= 20) { // 限制最多50页
                break;
            }
            
            $currentPage++;
            sleep($GLOBALS['SLEEP_TIME']);
            
        } while (true);
        
        return $processedCount;
        
    } catch (Exception $e) {
        echo "搜索商品出错：" . $e->getMessage() . "\n";
        return false;
    }
}

// 在处理完分类后标记为已处理
function markCategoryAsProcessed($db, $categoryId) {
    try {
        $sql = "UPDATE category SET is_sync = 1, updated_at = ? WHERE id = ?";
        $stmt = $db->getConnection()->prepare($sql);
        if (!$stmt) {
            throw new Exception("预处理SQL失败: " . $db->getConnection()->error);
        }
        
        $now = date('Y-m-d H:i:s');
        $stmt->bind_param('si', $now, $categoryId);
        
        if (!$stmt->execute()) {
            throw new Exception("更新分类状态失败: " . $stmt->error);
        }
        
        $stmt->close();
        return true;
    } catch (Exception $e) {
        echo "标记分类处理状态出错: " . $e->getMessage() . "\n";
        return false;
    }
}

// 开始爬取商品
try {
    echo "开始获取未处理的二级分类...\n";
    
    $categories = getLevel2Categories($db);
    
    if (!$categories) {
        throw new Exception("获取分类失败");
    }
    
    if ($categories->num_rows == 0) {
        throw new Exception("没有找到需要处理的分类");
    }
    $parent = [
        1 => '男装',//男装
        2 => '女装',//女装
        3 => '童装',//童装
        4 => '玩具',//玩具
        5 => '小家电'//小家电
    ];

    $totalProcessed = 0;
    while ($category = $categories->fetch_assoc()) {
        echo "\n开始处理分类: {$category['name']} (ID: {$category['id']})\n";
        $count = searchProducts(
            $sdk, 
            $db, 
            $category['name'], 
            $category['id'], 
            $accessToken,
            1 , // 从第1页开始,
            $parent[$category['parent_id']]
        );
        
        if ($count === false) {
            echo "处理分类 {$category['name']} 失败，跳过\n";
            continue;
        }
        
        $totalProcessed += $count;
        echo "分类 {$category['name']} 处理完成，共处理 {$count} 个商品\n";
        
        // 标记分类为已处理
        if (markCategoryAsProcessed($db, $category['id'])) {
            echo "分类 {$category['name']} 已标记为处理完成\n";
        }
        
        sleep($SLEEP_TIME);
    }
    
    echo "\n商品爬取完成！共处理 {$totalProcessed} 个商品\n";
} catch (Exception $e) {
    echo "发生错误：" . $e->getMessage() . "\n";
} finally {
    $db->close();
} 